<?php

App::uses('AppController', 'Controller');

/**
 * Carousels Controller
 *
 * @property Carousel $Carousel
 */
class CarouselsController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->Carousel->recursive = 0;
        $this->set('carousels', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->Carousel->exists($id)) {
            throw new NotFoundException(__('Invalid carousel'));
        }
        $options = array('conditions' => array('Carousel.' . $this->Carousel->primaryKey => $id));
        $this->set('carousel', $this->Carousel->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->Carousel->create();

            if (!empty($this->data)) {
                //Check if image has been uploaded
                if (!empty($this->data['Carousel']['upload'])) {
                    $file = $this->data['Carousel']['upload']; //put the data into a var for easy use

                    $ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
                    $arr_ext = array('jpg', 'jpeg', 'gif'); //set allowed extensions
                    //only process if the extension is valid
                    if (in_array($ext, $arr_ext)) {
                        //do the actual uploading of the file. First arg is the tmp name, second arg is 
                        //where we are putting it
                        move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img/carousel/' . $file['name']);

                        //prepare the filename for database entry
                        $this->request->data['Carousel']['src'] = $file['name'];
                    }
                }

                //now do the save
                if ($this->Carousel->save($this->request->data)) {
                    $this->Session->setFlash('The new carousel has been saved','default',array('class'=>'alert alert-info'));
                    $this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash('The new carousel could not be saved. Please, try again.','default',array('class'=>'alert alert-danger'));
                }
            }
        }
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        if (!$this->Carousel->exists($id)) {
            throw new NotFoundException('Invalid carousel','default',array('class'=>'alert alert-danger'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            $ok = false;
            if (!empty($this->data)) {
                //Check if image has been uploaded
                if (!empty($this->data['Carousel']['upload'])) {
                    $file = $this->data['Carousel']['upload']; //put the data into a var for easy use

                    $ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
                    $arr_ext = array('jpg', 'jpeg', 'gif', 'png'); //set allowed extensions
                    //only process if the extension is valid
                    if (in_array($ext, $arr_ext)) {
                        //do the actual uploading of the file. First arg is the tmp name, second arg is 
                        //where we are putting it
                        move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img/carousel/' . $file['name']);

                        //prepare the filename for database entry
                        $this->request->data['Carousel']['src'] = $file['name'];
                        $ok = true;
                    }else{
                        $ok = false;
                    }
                }

                //now do the save
                if ($ok == true) {
                    $this->Carousel->save($this->request->data);
                    $this->Session->setFlash('The carousel has been successfully edited','default',array('class'=>'alert alert-info'));
                    $this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash('The carousel could not be edited. Please, try again.','default',array('class'=>'alert alert-danger'));
                }
            }
        } else {
            $options = array('conditions' => array('Carousel.' . $this->Carousel->primaryKey => $id));
            $this->request->data = $this->Carousel->find('first', $options);
        }
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->Carousel->id = $id;
        if (!$this->Carousel->exists()) {
            throw new NotFoundException('Invalid carousel','default',array('class'=>'alert alert-danger'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Carousel->delete()) {
            $this->Session->setFlash('Carousel deleted','default',array('class'=>'alert alert-info'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash('Carousel was not deleted','default',array('class'=>'alert alert-danger'));
        $this->redirect(array('action' => 'index'));
    }

    public function show() {
        $condition = array('conditions' => array('Carousel.hidden' => 1));      
        $carousels = $this->Carousel->find('all', $condition);
         
        if (isset($this->params['requested'])) {
            return $carousels;
        }
        $this->set(compact('carousels', $carousels));
    }
    
    public function count(){
        $condition = array('conditions' => array('Carousel.hidden' => 1));
        $count = $this->Carousel->find('count', $condition);

        if (isset($this->params['requested'])) {
            return $count;
        }
        $this->set(compact('count', $count));
    }
    
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('show','count');
    }

}


